package com.suishoupai.xmb.Security;

import com.suishoupai.xmb.entity.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by caojian on 2017/3/21.
 */
public class MyUserDetail implements UserDetails {
    private Collection<GrantedAuthority> authorities;
    private User user;

    public MyUserDetail(User user) {
        this.user = user;
        String[] roles = user.getRole().split(",");
        Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        for (String role : roles) {
            SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(role);
            authorities.add(grantedAuthority);
        }
        this.authorities = authorities;
    }

    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public User getUser() {
        return user;
    }

    public String getPassword() {
        return user.getPassword();
    }

    public String getUsername() {
        return user.getEmail();
    }

    public boolean isAccountNonExpired() {
        return true;
    }

    public boolean isAccountNonLocked() {
        return true;
    }

    public boolean isCredentialsNonExpired() {
        return true;
    }

    public boolean isEnabled() {
        return true;
    }
}
